package service.api.ai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.service.OpenAiService;

import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.entity.chat.ChatCompletion;
import com.unfbx.chatgpt.entity.chat.ChatCompletionResponse;
import com.unfbx.chatgpt.entity.chat.Message;
import com.unfbx.chatgpt.entity.completions.CompletionResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import util.Request;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

@Slf4j
public class AiService {

    private static HashMap<String, Object> headerMap=new HashMap<>();

    public static void main(String[] args) throws Exception{
//        OpenAiService service = new OpenAiService("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");
//        CompletionRequest completionRequest = CompletionRequest.builder()
//                .prompt("Somebody once told me the world is gonna roll me")
//                .model("ada")
//                .echo(true)
//                .build();
//        service.createCompletion(completionRequest).getChoices().forEach(System.out::println);

//        OpenAiClient openAiClient = new OpenAiClient("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");
//        CompletionResponse completions = openAiClient.completions("三体人是什么？");
//        Arrays.stream(completions.getChoices()).forEach(System.out::println);

//        ChatGPTClient client = new ChatGPTClient("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");
//        String body = client.askQuestion("简单描述下三体这本书");
//        System.out.println(body);
//
//        OpenAiClient openAiClient = OpenAiClient.builder()
//                .apiKey(Collections.singletonList("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"))
                //自定义key的获取策略：默认KeyRandomStrategy
                //.keyStrategy(new KeyRandomStrategy())
//                .keyStrategy(new FirstKeyStrategy())
                //自己做了代理就传代理地址，没有可不不传
//                .apiHost("http://153578456:handsomeboy666@175.178.230.75:7890")
//                .build();
        //聊天模型：gpt-3.5
//        Message message = Message.builder().role(Message.Role.USER).content("你好啊我的伙伴！").build();
//        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Collections.singletonList(message)).build();
//        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
//        chatCompletionResponse.getChoices().forEach(e -> {
//            System.out.println(e.getMessage());
//        });

        HashMap<String, Object> headerMap=new HashMap<>();
        headerMap.put("Content-Type","application/json");
        headerMap.put("Authorization","Bearer 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");
        String data="{\"model\":\"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\",\"content\": \"你好\"}]}";
        System.out.println(Request.requestByProxy("https://api.openai.com/v1/chat/completions",headerMap,data));
    }

    public static String singleChat(String ask) throws Exception{
        headerMap.put("Content-Type","application/json");
        headerMap.put("Authorization","Bearer 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");
        String data="{\"model\":\"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\",\"content\": \""+ ask +"\"}]}";
        Object response=Request.requestByProxy("https://api.openai.com/v1/chat/completions",headerMap,data);
        String jsonStr= JSON.parseObject(response.toString()).getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
        return jsonStr;
    }

    public static JSONArray imageChat(String ask) throws Exception{
        headerMap.put("Content-Type","application/json");
        headerMap.put("Authorization","Bearer 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");
        String data="{\"prompt\":\""+ ask +"\", \"n\": 1,\"size\":\"512x512\"}";
        Object response=Request.requestByProxy("https://api.openai.com/v1/images/generations",headerMap,data);
        JSONArray jsonArray= JSON.parseObject(response.toString()).getJSONArray("data");
        return jsonArray;
    }

    public static String continuousChat(String ask) throws Exception{
        headerMap.put("Content-Type","application/json");
        headerMap.put("Authorization","Bearer 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");
        String data="{\"model\":\"gpt-3.5-turbo\", \"messages\": "+ ask +"}";
        log.info("data:"+ StringUtils.replace(data,"\n","\\n"));
        Object response=Request.requestByProxy("https://api.openai.com/v1/chat/completions",headerMap,StringUtils.replace(data,"\n","\\n"));
        String jsonStr= JSON.parseObject(response.toString()).getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
        return jsonStr;
    }
}
